import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
const {TableStore} = WeaTableNew;
import {message, Button, Modal} from "antd";
import * as Apis from '../../apis/info';
import { ListStore } from '../listStore';
import { infoDialogStore } from '../dialog/infoDialogStore';
import {Text} from '../../components/util/text';
const text = new Text();
const getLabel = WeaLocaleProvider.getLabel;
export default class infoReportListStore extends ListStore {

    @observable infoDialogStore = new infoDialogStore(this);

    type = "";

    @observable responseData = {};
    @observable isRight = false;        //判断是否有权限
    @observable loadingSpin = true;     //全局loading
    @observable selectTabKey = "";    
    @observable tabs = [];          //regTop展示页签
    @observable status = {
        loading: false
    }

    constructor(props) {
        super(props);
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    //得到上报信息list,高级搜索condition初始化
    @action
    getInfoReportList = (params = {}) => {
        this.type = params.type;
        this.status.loading = true;
        let newparams = {...params };
        //this.loadingSpin = true;
        //this.tabs = [];
        this.searchForm = new WeaForm();
        this.searchCondition = [];
        Apis.getInfoReportList(newparams).then(data => {
            if (data.api_status == '1') {
                this.loadingSpin = false;
                this.isRight = data.isRight;
                if (!this.isRight) {
                    return;
                }
                if (this.tabs.length == 0) {
                    // if (data.pathList.length > 1 && this.type != 'auditReport') {
                    //     this.tabs.push({ key: 'all', title: text.ALLPATH });
                    // }
                    data.pathList.map(m => {
                        this.tabs.push({ key: m.id, title: m.name });
                        this.selectTabKey = data.pathList[0].id;
                    });
                }
                this.tableStore.getDatas(data.sessionkey, 1);
                this.responseData = { rightMenus: data.rightMenus };
                this.searchCondition = data.infoConditionGroup;
                this.searchForm && !this.searchForm.isFormInit && this.searchForm.initFormFields(data.infoConditionGroup);
                this.title = data.title;
                this.status.loading = false;
            } else {
                message.error(data.api_errormsg);
            }
        })
    }

    @action
    doSearch = (params = {}) => {
        this.status.loading = true;
        //获取表单的参数值
        const searchParamsAd = this.searchForm.getFormParams();
        const newParams = {...searchParamsAd, 'type': this.type,'pathId': this.selectTabKey };
        Apis.getInfoReportList(newParams).then(data => {
            this.tableStore = new TableStore();
            this.tableStore.getDatas(data.sessionkey, 1);
            this.status.loading = false;
        })

        // Apis.getInfoSearchList(newParams).then(data => {
        //     this.tableStore = new TableStore();
        //     this.tableStore.getDatas(data.sessionkey, 1);
        //     this.status.loading = false;
        // })
    }

    //设置选中按钮
    @action
    changeTab = (key) => {
        this.selectTabKey = key;
        let newparams = {};
        // if (key == "all") {              //基本信息
        //     this.getInfoReportList({ 'type': this.type });
        // } else {
            this.getInfoReportList({ 'type': this.type, 'pathId': this.selectTabKey });
        // }
    }

    getRightMenu = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map(m => {
            btnArr.push({
                key: m.type,
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                onClick: key => {
                    this.buttonClick(key);
                },
                disabled: m.type == "BTN_SUBMIT_BATCH" ? this.tableStore.selectedRowKeys.length === 0 : false
            })
        });
        return btnArr;
    }
    /**
   * 上方按钮
   */
    getTopButton = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map((m,index) => {
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@f9xgj8@${index}`} style={{ marginLeft: "10px" }} type={"primary"}
                    disabled={m.type == "BTN_SUBMIT_BATCH" ? this.tableStore.selectedRowKeys.length === 0 : false}
                    onClick={() => {
                        this.buttonClick(m.type);
                    } }>
                    {m.menuName}
                </Button>
            );
        });
        return btnArr;
    }

    buttonClick(key) {
        if (key == "BTN_CREATE") {
            this.infoDialogStore.initDialogForm();   //初始化弹出框表单数据
            //this.infoDialogStore.setState({ visible: true });
        }
        if (key == "BTN_SUBMIT_BATCH") {
            const _this = this;
            Modal.confirm({
                content: text.SURESUBMITAUDIT,
                onOk() {
                    let selectedRowKeys = toJS(_this.tableStore.selectedRowKeys);
                    Apis.submitInfoReport({ ids: selectedRowKeys }).then(data => {
                        if (data.api_status == '1') {
                            if (!data.isRight) {
                                message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                                return;
                            }
                            message.success(text.SUBMITAUDIT);
                            _this.tableStore = new TableStore();
                            _this.getInfoReportList({ type: 'waitReport' });
                        } else {
                            message.error(data.api_errormsg);
                        }
                    });
                },
                onCancel() { },
            });

        }
    }
}